﻿using System;
using VNet.VisiSuite.BusinessLogic;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message.Base;

namespace VNet.VisiSuite.Service.Helper
{
    public static class ServiceChecker
    {
        private static SecurityUtil _securityUtil = new SecurityUtil();

        public static void ValidateInput(this ServiceBaseRequest request, string operationName)
        {
            request.RequiredNotNull();
            request.Identity.RequiredNotNull();
            request.Identity.PopulateIdentityToken();
            request.Identity.ValidateIdentity(operationName);
        }

        public static void PopulateIdentityToken(this UserIdentity user)
        {
            var userIdentity = _securityUtil.GetUserTransportToken(user.UserLoginId);
            if (userIdentity == null)
                throw new ArgumentNullException("userId");

            UserTransportToken.UserIdentity.UserLoginId = userIdentity.UserLoginId;
            UserTransportToken.UserIdentity.Token = userIdentity.Token;
            UserTransportToken.UserIdentity.IPAddress = userIdentity.IPAddress;
        }

        public static void RequiredNotNull(this object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
        }

        public static void ValidateIdentity(this UserIdentity identity, string message)
        {
            // Check token
            RequireToken(identity.Token, message);

            // Check IP Address
            RequiredIPAddress(identity.IPAddress, message);
        }

        public static void RequireToken(string token, string message)
        {
            string userToken = UserTransportToken.UserIdentity.Token;
            if (string.IsNullOrEmpty(userToken))
            {
                string msg = "User Transport Token is not present" + message;
                FileLog.Error("RequireToken() - " + msg);
                throw new InvalidOperationException(msg);
            }
            if (token != userToken)
            {
                string msg = "Verifed token failed" + message;
                FileLog.Error("RequireToken() - " + msg);
                // throw new InvalidOperationException(msg);
            }
        }

        public static void RequiredIPAddress(string ipAddress, string message)
        {
            string userIPAddress = UserTransportToken.UserIdentity.IPAddress;
            if (string.IsNullOrEmpty(userIPAddress))
            {
                throw new InvalidOperationException("User Transport Token is not present" + message);
            }
            if (ipAddress != userIPAddress)
            {
                throw new InvalidOperationException("Verifed token failed" + message);
            }
        }

        public static void ValidateInput(this VisiBoardServiceBaseRequest request, string operationName)
        {
            request.RequiredNotNull();
        }

    }
}